<?php

namespace app\common\services;

use app\models\IntegralGood;
use app\models\IntegralRecord;
use app\models\Member;
use app\models\Recommend;
use app\models\User;
use yii\helpers\Url;

class StaticServices {
  public static $page = 50;

  public static function getUserRole()
  {
    return ['1' => '大众经纪人', '2' => '案场经理', '3' => '置业顾问', '4' => '房产中介'];
  }

  public static function getIntegral()
  {
    return [ '邀请好友' => 100, '参加活动' => 100, '案场销售赠送' => 500, '会员邀请并成功认购' => 2000, '其他' => 0];
  }


  public static function checkIntegralSource($source)
  {
    $integral_arr = [ '邀请好友' => 100, '参加活动' => 100, '案场销售赠送' => 500, '会员邀请并成功认购' => 2000, '其他' => 0];

    if (array_key_exists($source, $integral_arr)) {
      return true;
    }
    return false;
  }

  public static function getGoods($id)
  {
    $member_info = Member::find()->where(['id' => $id])->asArray()->one();
    $goods_info = IntegralGood::find()->select(['id', 'good_name'])->where(['<=', 'good_integral', $member_info['member_integral']])->orderBy(['create_time' => SORT_DESC])->asArray()->all();
    return $goods_info;
  }

  public static function getMemberUseIntegral($id)
  {
    $member_user_intgral = IntegralRecord::find()->where(['member_id' => $id])->sum('good_integral');
    return $member_user_intgral;
  }

  public static function includeAppJs($path, $depend)
  {
    return self::includeApp('js', $path, $depend);
  }

  public static function includeAppCss($path, $depend)
  {
    return self::includeApp('css', $path, $depend);
  }

  private static function includeApp($type, $path, $depend)
  {
    if ($type == 'js') {
      \Yii::$app->getView()->registerJsFile($path, ['depends' => $depend]);
    }
    if ($type == 'css') {
      \Yii::$app->getView()->registerCssFile($path, ['depends' => $depend]);
    }
  }

  public static function getRandChar($length = 6)
  {
    $str = null;
    $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($strPol) - 1;

    for ($i = 0; $i < $length; $i++) {
      $str .= $strPol[rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
    }

    return $str;
  }

  //mt_rand(100000, 999999)
  public static function getCode($code, $template, $phone, $code_type)
  {
    $shortMessage = new ShortMessageServices();
    $shortMessage->CreateCode($code, mt_rand(100, 999), $template, '宁波漫乐荟', $phone, $code_type);
  }

  public static function getIP()
  {
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
  }

  public static function buildMUrl($path, $params = [])
  {
    $domain = \Yii::$app->params['domain'];
    $path = Url::toRoute(array_merge([$path], $params));

    return $domain['m'] . $path;
  }
}